package com.bluesky.common.convert;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bluesky.common.Constants;
import com.bluesky.common.utils.DictUtils;

import java.lang.reflect.Field;

/**
 * Excel数据字典转换器
 * 只支持sys_和enum_开头的字典
 *
 * @author Kevin
 */
public class ExcelDictConverter implements Converter<String> {

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Field field = excelContentProperty.getField();
        String dictValue = transformDictValue(field.getName(), cellData.getStringValue());
        return StrUtil.isBlank(dictValue) ? cellData.getStringValue() : dictValue;
    }

    @Override
    public CellData convertToExcelData(String s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Field field = excelContentProperty.getField();
        String dictLabel = transformDictLabel(field.getName(), s);
        return new CellData(StrUtil.isBlank(dictLabel) ? s : dictLabel);
    }

    /**
     * 字段value，翻译成字典label
     *
     * @param fieldName
     * @param fieldValue
     * @return
     */
    private String transformDictLabel(String fieldName, String fieldValue) {
        String dictLabel = transformSysDictLabel(fieldName, fieldValue);
        if (StrUtil.isNotBlank(dictLabel)) {
            return dictLabel;
        }
        dictLabel = transformEnumDictLabel(fieldName, fieldValue);
        return dictLabel;
    }

    private String transformSysDictLabel(String fieldName, String fieldValue) {
        String dictCode = StrUtil.toUnderlineCase(String.format(Constants.SYS_DICT_KEY_TPL, fieldName));
        String dictLabel = DictUtils.getDictLabel(dictCode, fieldValue);
        return dictLabel;
    }

    private String transformEnumDictLabel(String fieldName, String fieldValue) {
        String dictCode = StrUtil.toUnderlineCase(String.format(Constants.ENUM_DICT_KEY_TPL, fieldName));
        String dictLabel = DictUtils.getDictLabel(dictCode, fieldValue);
        return dictLabel;
    }

    /**
     * 字段value，翻译成字典value
     *
     * @param fieldName
     * @param fieldValue
     * @return
     */
    private String transformDictValue(String fieldName, String fieldValue) {
        String dictValue = transformSysDictValue(fieldName, fieldValue);
        if (StrUtil.isNotBlank(dictValue)) {
            return dictValue;
        }
        dictValue = transformEnumDictValue(fieldName, fieldValue);
        return dictValue;
    }

    private String transformSysDictValue(String fieldName, String fieldValue) {
        String dictCode = StrUtil.toUnderlineCase(String.format(Constants.SYS_DICT_KEY_TPL, fieldName));
        String dictValue = DictUtils.getDictValue(dictCode, fieldValue);
        return dictValue;
    }

    private String transformEnumDictValue(String fieldName, String fieldValue) {
        String dictCode = StrUtil.toUnderlineCase(String.format(Constants.ENUM_DICT_KEY_TPL, fieldName));
        String dictValue = DictUtils.getDictValue(dictCode, fieldValue);
        return dictValue;
    }

}
